package cn.wyj.dao;

import cn.wyj.domain.Article;
import cn.wyj.domain.Statistic;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StatisticMapper {

    /**
     * 新增文章对应的统计信息
     * @param article
     */
    @Insert("insert into statistic (article_id,hits,comments_num) values(#{id},0,0)")
    void addStatistic(Article article);

    /**
     * 根据文章id查询对应的统计信息
     * @param ArticleId
     * @return
     */
    @Select("select *from statistic where article_id =#{ArticleId}")
    Statistic findStatisticByArticleId(Integer ArticleId);

    /**
     * 根据文章id更新点击量信息
     * @param ArticleId
     */
    @Update("update statistic set hits = #{hits} where article_id = #{articleId}")
    void updateArticleHitsByArticleId(Integer ArticleId);

    /**
     * 根据文章id更新评论量信息
     * @param statistic
     */
    @Update("update statistic set comments_num = #{commentsNum} where article_id = #{articleId}")
    void updateArticleCommentsByArticleId(Statistic statistic);

    /**
     * 根据文章id删除对应统计信息
     * @param articleId
     */
    @Delete("delete from statistic where article_id = #{articleId}")
    void delStatisticByArticleId(Integer articleId);

    /**
     * 统计文章热度信息
     * @return
     */
    @Select("select *from statistic order by hits DESC,comments_num DESC")
    List<Statistic> getStatistic();

    /**
     * 统计博客文章总的访问量
     * @return
     */
    @Select("select sum(hits) from statistic")
    long getTotalHits();


    /**
     * 统计博客文章总的评论量
     * @return
     */
    @Select("select sum(comments_num) from statistic")
    long getTotalComments();
}
